package com.coin900.coin.service.fu;

import com.coin900.coin.base.CoinConstants;
import com.coin900.coin.persist.fu.*;
import com.value.mybatis.IDaoRouter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资产明细
 *
 * @author shui
 * @create 2017-12-18
 */
@Service
public class FuCapitalDetailService implements IFuCapitalDetailService {

    @Override
    public void insertFuCapitalDetail(FuCapitalDetailVO fuCapitalDetailVO) {
        daoRouter.insert("FuCapitalDetail.insertFuCapitalDetail", fuCapitalDetailVO);
    }

    @Override
    public void doCapitalWithdrawSendToAddress(FuCapitalDetailVO fuCapitalDetailVO) {
        this.updateFuCapitalDetail(fuCapitalDetailVO);
        FuCapitalTransactionVO fuCapitalTransactionVO = new FuCapitalTransactionVO();
        fuCapitalTransactionVO.setTxid(fuCapitalDetailVO.getHash());
        fuCapitalTransactionVO.setType(2);
        fuCapitalTransactionVO.setIsSuccess(2);
        fuCapitalTransactionVO.setCreatedDate(new Date());
        fuCapitalTransactionVO.setDataStatus(1);
        this.transactionService.insertFuCapitalTransaction(fuCapitalTransactionVO);
    }

    @Override
    public void doRechargeConfirmSuccess(FuCapitalTransactionVO transactionVO) {
        // 更新确认状态为成功
        this.transactionService.updateFuCapitalTransaction(transactionVO);
        Date date = new Date();
        // 充值成功之后的处理

        String toAddress = transactionVO.getToAddress();
        Double verify = transactionVO.getVerify();
        String txid = transactionVO.getTxid();

        // 根据充值地址查询用户钱包，钱包金额变化
        Map<String, Object> params = new HashMap<>();
        params.put("address", toAddress);
        params.put("dataStatus", 1);
        List<FuUserWalletVO> fuUserWalletVOS = this.walletService.loadFuUserWallet(params);
        FuUserWalletVO fuUserWalletVO = fuUserWalletVOS.get(0);
        fuUserWalletVO.setBalance(verify);
        fuUserWalletVO.setAvailable(verify);
        fuUserWalletVO.setModifiedDate(date);
        fuUserWalletVO.setCreatedDate(date);
        this.walletService.doRechargeSuccess(fuUserWalletVO);

        // 新增充值记录
        FuCapitalRechargeVO fuCapitalRechargeVO = new FuCapitalRechargeVO();
        fuCapitalRechargeVO.setCoinId(fuUserWalletVO.getCoinId());
        fuCapitalRechargeVO.setBalance(verify);
        fuCapitalRechargeVO.setCreatedDate(date);
        fuCapitalRechargeVO.setDataStatus(1);
        this.rechargeService.insertFuCapitalRecharge(fuCapitalRechargeVO);

        FuCapitalDetailVO fuCapitalDetailVO = new FuCapitalDetailVO();
        fuCapitalDetailVO.setUserId(fuUserWalletVO.getUserId());
        fuCapitalDetailVO.setAddress(fuUserWalletVO.getAddress());
        fuCapitalDetailVO.setType(1);
        fuCapitalDetailVO.setTargetId(fuCapitalRechargeVO.getId());
        fuCapitalDetailVO.setVerify(verify);
        fuCapitalDetailVO.setHash(txid);
        fuCapitalDetailVO.setTransferStatus(CoinConstants.COIN_CAPITAL_CONFIRM_WATIONG);
        fuCapitalDetailVO.setCreatedDate(date);
        fuCapitalDetailVO.setDataStatus(1);
        this.insertFuCapitalDetail(fuCapitalDetailVO);
    }

    @Override
    public void doWithdrawConfirmSuccess(FuCapitalTransactionVO transactionVO) {
        // 更新确认状态为成功
        this.transactionService.updateFuCapitalTransaction(transactionVO);
        Date date = new Date();
        // 提现成功之后的处理
        // 根据 hash 更新这次提现
        String txid = transactionVO.getTxid();
        Double verify = transactionVO.getVerify();
        Map<String, Object> params = new HashMap<>();
        params.put("txid", txid);
        params.put("dataStatus", 1);
        List<FuCapitalDetailVO> fuCapitalDetailVOS = this.loadFuCapitalDetail(params);
        FuCapitalDetailVO dbCapitalDetailVO = fuCapitalDetailVOS.get(0);

        FuCapitalDetailVO fuCapitalDetailVO = new FuCapitalDetailVO();
        fuCapitalDetailVO.setId(dbCapitalDetailVO.getId());
        fuCapitalDetailVO.setVerify(verify);
        fuCapitalDetailVO.setTransferStatus(CoinConstants.COIN_CAPITAL_CONFIRM_SUCCESS);
        fuCapitalDetailVO.setModifiedDate(date);
        fuCapitalDetailVO.setConfirmTime(date);
        this.updateFuCapitalDetail(fuCapitalDetailVO);

        Long targetId = dbCapitalDetailVO.getTargetId();
        params.clear();
        params.put("id", targetId);
        params.put("dataStatus", 1);
        List<FuCapitalWithdrawVO> fuCapitalWithdrawVOS = this.withdrawService.loadFuCapitalWithdraw(params);
        FuCapitalWithdrawVO fuCapitalWithdrawVO = fuCapitalWithdrawVOS.get(0);
        // 提现成功扣除锁定
        Long coinId = fuCapitalWithdrawVO.getCoinId();
        FuUserWalletVO fuUserWalletVO = new FuUserWalletVO();
        fuUserWalletVO.setCoinId(coinId);
        fuUserWalletVO.setUserId(dbCapitalDetailVO.getUserId());
        fuUserWalletVO.setBlocked(verify);
        fuUserWalletVO.setModifiedDate(date);
        this.walletService.doRechargeSuccess(fuUserWalletVO);
        this.walletService.doWithdrawPayed(fuUserWalletVO);
    }

    @Override
    public void updateFuCapitalDetail(FuCapitalDetailVO fuCapitalDetailVO) {
        daoRouter.update("FuCapitalDetail.updateFuCapitalDetail", fuCapitalDetailVO);
    }

    @Override
    public void deleteFuCapitalDetail(FuCapitalDetailVO fuCapitalDetailVO) {
        daoRouter.update("FuCapitalDetail.deleteFuCapitalDetail", fuCapitalDetailVO);
    }

    @Override
    public List<FuCapitalDetailVO> loadFuCapitalDetail(Map<String, Object> params) {
        return daoRouter.query("FuCapitalDetail.loadFuCapitalDetail", params);
    }

    @Autowired
    private IDaoRouter daoRouter;
    @Autowired
    private IFuCapitalTransactionService transactionService;
    @Autowired
    private IFuCapitalRechargeService rechargeService;
    @Autowired
    private IFuCapitalWithdrawService withdrawService;
    @Autowired
    private IFuUserWalletService walletService;

}